<template>
	<view class="content">
		<!-- <uni-popup ref="popup" @change="change">
			<view class="popup-content">
				<view class="warp">
					<button class="button" type="primary" @click="setFormType(1)">吊装令</button>
					<button class="button" type="primary" @click="setFormType(2)">动火令</button>
					<button class="button" type="primary" @click="setFormType(3)">动土令</button>
				</view>
			</view>
		</uni-popup> -->
		<view class="content-form">
			<uni-forms ref="baseForm" :rules="rules" :modelValue="formData" label-position="top">
				<uni-forms-item label="施工部位" required name="constructionSite">
					<uni-easyinput v-model="formData.constructionSite" placeholder="请输入施工部位" />
				</uni-forms-item>
				<uni-forms-item label="作业班组" required name="workTeam">
					<!-- <uni-data-picker placeholder="请选择作业班组" popup-title="请选择作业班组" :localdata="dataTree"
						v-model="formData.workTeam">
					</uni-data-picker> -->
					<uni-easyinput v-model="formData.workTeam" placeholder="请输入作业班组" />
				</uni-forms-item>
				<uni-forms-item label="申请事由" required name="reasonForApplication">
					<uni-easyinput type="textarea" v-model="formData.reasonForApplication" placeholder="请输入申请事由" />
				</uni-forms-item>
				<uni-forms-item label="开始时间" required name="startTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.startTime"
						placeholder="请选择开始时间" />
				</uni-forms-item>
				<uni-forms-item label="结束时间" required name="endTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.endTime"
						placeholder="请选择结束时间" />
				</uni-forms-item>

				<uni-forms-item label="动火类型" required name="hotWorkType">
					<uni-data-picker placeholder="请选择动火类型" popup-title="请选择动火类型" :localdata="hotWorkList"
						v-model="formData.hotWorkType">
					</uni-data-picker>
				</uni-forms-item>

				<uni-forms-item label="动火等级" required name="level">
					<uni-data-picker placeholder="请选择动火等级" popup-title="请选择" :localdata="levelList"
						v-model="formData.level">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="作业人员" required name="operativesId">
					<uni-data-picker placeholder="请选择操作人员" popup-title="请选择操作人员" :localdata="userList"
						v-model="formData.operativesId" @change="e=>getSelectUserName(e,'operatives')">
					</uni-data-picker>
					<!-- <uni-easyinput v-model="formData.operatives" placeholder="请输入作业人员" /> -->
				</uni-forms-item>

				<uni-forms-item label="作业证件" required name="homeworkCertificate">
					<uni-file-picker v-model="imageValue" limit="3" title="最多选择3张图片" @select="select"
						@delete="delIMG"></uni-file-picker>
				</uni-forms-item>


				<uni-forms-item label="现场监护人" required name="guardianId">
					<uni-data-picker placeholder="请选择监护人员" popup-title="请选择监护人员" :localdata="userList"
						v-model="formData.guardianId" @change="e=>getSelectUserName(e,'guardianName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="班组负责人" required name="applicationManagerId">
					<uni-data-picker placeholder="请选择班组负责人" popup-title="请选择班组负责人" :localdata="userList"
						v-model="formData.applicationManagerId"
						@change="e=>getSelectUserName(e,'applicationManagerName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-section class="mb-10" title="安全措施" type="line"></uni-section>
				<view class="jcydBox" v-for="(item,index) in formData.remark">
					<view class="jcydItem">
						<view class="text">
							{{item.text}}
						</view>
						<uni-data-checkbox v-model="item.value"
							:localdata="[{text:'是',value:'1'},{text:'否',value:'2'}]"></uni-data-checkbox>
					</view>
					<view class="xian">

					</view>
				</view>
			</uni-forms>
			<button type="primary" @click="submit('baseForm')">提交</button>

		</view>

	</view>
</template>

<script>
	import {
		listUser
	} from "@/api/system/user.js"
	import {
		addHomework
	} from "@/api/homework";
	import config from '@/config'
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				show: false,
				formData: {
					id: null,
					type: 2,
					applicationTime: null,
					constructionSite: null,
					workTeam: null,
					liftingEquipment: null,
					equipmentCode: null,
					reasonForApplication: null,
					hotWorkType: null,
					level: null,
					startTime: null,
					endTime: null,
					operatives: null,
					depth: null,
					supportMethod: null,
					peopleCount: null,
					homeworkCertificate: [],
					commandId: null,
					commandName: null,
					guardianId: null,
					guardianName: null,
					applicationManagerId: null,
					applicationManagerName: null,
					technicalDepartment: null,
					securitySector: null,
					projectLeader: null,
					status: 0,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					remark: [{
							text: "1、五级（含五级）以上风力时，应停止室外动火作业。确需动火作业时，应采取可靠的挡风措施。",
							value: 0,
						},
						{
							text: "2、设备工具工况（气瓶使用是否符合安全标准，设备电源、安全附件是否有效）。",
							value: 0,
						},
						{
							text: "3、动火作业人员是否进行安全教育培训及技术交底。",
							value: 0,
						},
						{
							text: "4、作业人员劳动防护用品配备是否符合安全作业要求。",
							value: 0,
						},
						{
							text: "5、动火地点周边及下方是可燃物是否清理干净。",
							value: 0,
						},
						{
							text: "6、现场安全防护措施是否设置到位，高处动火作业是否设置防火花飞溅安全措施。",
							value: 0,
						},
						{
							text: "7、动火地点是否按规定配备消防器材。",
							value: 0,
						},
						{
							text: "8、是否制定火灾现场处置方案。",
							value: 0,
						},
						{
							text: "9、其他相关特殊作业已办理相应安全作业票，作业现场四周已设立警戒区。",
							value: 0,
						},
					],
				},
				// 表单校验
				rules: {
					constructionSite: {
						rules: [{
							required: true,
							errorMessage: "施工部位不能为空",

						}]
					},
					workTeam: {
						rules: [{
							required: true,
							errorMessage: "作业班组不能为空",

						}]
					},
					liftingEquipment: {
						rules: [{
							required: true,
							errorMessage: "起重设备不能为空",

						}]
					},
					equipmentCode: {
						rules: [{
							required: true,
							errorMessage: "设备编号不能为空",

						}]
					},
					reasonForApplication: {
						rules: [{
							required: true,
							errorMessage: "申请事由不能为空",

						}]
					},
					hotWorkType: {
						rules: [{
							required: true,
							errorMessage: "动火类型不能为空",
							trigger: "change"
						}]
					},
					level: {
						rules: [{
							required: true,
							errorMessage: "级别不能为空",

						}]
					},
					startTime: {
						rules: [{
							required: true,
							errorMessage: "开始时间不能为空",

						}]
					},
					endTime: {
						rules: [{
							required: true,
							errorMessage: "结束时间不能为空",

						}]
					},
					operativesId: {
						rules: [{
							required: true,
							errorMessage: "作业人员不能为空",

						}]
					},
					depth: {
						rules: [{
							required: true,
							errorMessage: "深度不能为空",

						}]
					},
					supportMethod: {
						rules: [{
							required: true,
							errorMessage: "支护方式不能为空",

						}]
					},
					peopleCount: {
						rules: [{
							required: true,
							errorMessage: "作业人数不能为空",

						}]
					},
					homeworkCertificate: {
						rules: [{
							required: true,
							errorMessage: "作业证件不能为空",

						}]
					},
					commandId: {
						rules: [{
							required: true,
							errorMessage: "指挥人不能为空",

						}]
					},
					guardianId: {
						rules: [{
							required: true,
							errorMessage: "监护人不能为空",

						}]
					},
					applicationManagerId: {
						rules: [{
							required: true,
							errorMessage: "申请班组负责人不能为空",

						}]
					},
				},
				dataTree: [{
						text: "木工班组",
						value: "木工班组",
					},
					{
						text: "架子工班组",
						value: "架子工班组",
					},

				],
				levelList: [{
					text: "一级",
					value: "1",
				}, {
					text: "二级",
					value: "2",
				}, {
					text: "三级",
					value: "3",
				}, ],
				userList: [],
				hotWorkList: [{
					text: "电焊",
					value: "1",
				}, {
					text: "气焊",
					value: "2",
				}, {
					text: "气割",
					value: "3",
				}, {
					text: "热熔",
					value: "4",
				}, {
					text: "其他",
					value: "5",
				}, ],
				imageValue: [],

			}
		},
		mounted() {


			this.getData()
		},
		methods: {
			change(e) {
				this.show = e.show
			},
			setFormType(value) {
				this.formData.type = value
				this.$refs.popup.close()
			},
			getData() {
				listUser().then((res) => {
					this.userList = res.data.map((item) => {
						return {
							text: item.nickName,
							value: item.userId,
							deptId: item.deptId
						}
					})
				})
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					this.formData.remark = JSON.stringify(this.formData.remark)
					this.formData.homeworkCertificate = this.formData.homeworkCertificate.join(',')
					addHomework(this.formData).then((response) => {
						this.$modal.msgSuccess("新增成功");
						this.$tab.reLaunch('/pages/index')
					});
				}).catch(err => {
					console.log('err', err);
				})
			},
			getSelectUserName(value, key) {

				this.formData[key] = value.detail.value[0].text
				console.log(this.formData)
			},
			//图片上传
			select(e) {
				console.log(e);
				const tempFilePaths = e.tempFilePaths;
				let that = this

				tempFilePaths.forEach(item => {
					this.imageValue.push({
						url: item.path,
						name: ""
					})

					uni.uploadFile({
						url: config.baseUrl + "/common/upload",
						// 上传的文件
						filePath: item,
						name: 'file',

						// 请求头，设置token
						header: {
							'Authorization': getToken()
						},
						// 上传成功的回调函数
						success: (uploadFileRes) => {
							let res = JSON.parse(uploadFileRes.data)
							if (res.code == '200') {
								that.formData.homeworkCertificate.push(res.fileName)
								that.imageValue.pop()
								that.imageValue.push({
									url: config.baseUrl + res.fileName,
									name: res.fileName
								})
								console.log(that.imageValue);
							} else {
								that.imageValue.pop()
								uni.showToast({
									title: "上传失败",
									icon: "none"
								})
							}

						},
						fail: error => {

						}
					});
				})

			},
			//图片删除
			delIMG(e) {


				this.imageValue.splice(e.index, 1);
				this.formData.homeworkCertificate.splice(e.index, 1);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #fff;
		padding: 15px;
		width: 300px;

		.warp {

			.button {
				background-color: #248aff;
				margin-bottom: 10px;
			}
		}


	}

	.content-form {
		padding: 15px;
		background-color: #fff;
	}

	::v-deep .uni-forms-item {
		margin-bottom: 17px;
	}

	::v-deep .uni-forms-item__label {
		width: 180px !important;
	}

	.jcydBox {
		margin: 10px 0 0 0;

		.jcydItem {
			margin-bottom: 10px;

			.text {
				margin-bottom: 5px;
			}
		}

		.xian {
			width: 100%;
			height: 1px;
			background-color: #b9b9b9;
		}
	}
</style>